package thread;

class MyBlockingQueue1{
    private String[] data = null;

    //队首
    private int head = 0;
    //队尾
    private int tail = 0;
    //元素个数
    private int size = 0;

    public MyBlockingQueue1(int capacity){
        data = new String[capacity];
    }

    //入队
    public void put(String elem) throws InterruptedException {
        synchronized (this){
            while (size >= data.length){
                //队列满了，需要阻塞
                //return;
                this.wait();
            }
            data[tail] = elem;
            tail++;
            if (tail >= data.length){
                tail = 0;
            }
            //也可以用
            //tail = (tail +1) % data.length;

            size++;
            this.notify();//生产者和消费者不会同时阻塞等待，入队的notify结束出队的wait，出队的notify结束入队的wait
        }
    }

    //出队
    public String take() throws InterruptedException {
        synchronized (this){
            while(size == 0){
                //队列空了，需要阻塞
                //return null;
                this.wait();
            }
            String ret = data[head];
            head++;
            if (head >= data.length){
                head = 0;
            }
            size--;
            this.notify();
            return ret;
        }
    }

}

public class Demo33 {
    public static void main(String[] args) {
        MyBlockingQueue1 queue = new MyBlockingQueue1(1000);

        Thread producer = new Thread(()-> {
            int n = 0;
            while(true){
                try {
                    queue.put(n+"");
                    System.out.println("生产元素："+ n);
                    n++;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"producer");

        Thread consumer = new Thread(()->{
            while(true){
                String n = null;
                try {
                    n = queue.take();
                    System.out.println("消费元素：" + n);
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"consumer");

        producer.start();
        consumer.start();
    }
}
